package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.items.entity.Zxjpk;
import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import com.gfxy.modules.items.mapper.ZxjpkMapper;
import com.gfxy.modules.items.service.IZxjpkService;
import com.gfxy.modules.items.vo.*;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 在线精品课
 * @Author: jeecg-boot
 * @Date:   2021-09-23
 * @Version: V1.0
 */
@Service
public class ZxjpkServiceImpl extends AbstractProjectServiceImpl<ZxjpkMapper, Zxjpk> implements IZxjpkService {

	protected int memberCellNum = 5;

	@Override
	protected void pass(String id, String isPass) {

	}

	@Override
	protected String[] getMemberArray(Row row) {
		String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
		return nameStrs.split(memberNameSeparator);
	}

	@Override
	protected Zxjpk getOneProject(Row row) {

		String date = ExcelUtil.formatCell(row.getCell(0));

		String award= ExcelUtil.formatCell(row.getCell(1));
		String classess  = ExcelUtil.formatCell(row.getCell(2));
		String projectName = ExcelUtil.formatCell(row.getCell(4));
		String stepName = ExcelUtil.formatCell(row.getCell(3));

		Zxjpk zxjpk = new Zxjpk();

		zxjpk.setDate(date);
		zxjpk.setAward(translateAward(award));
		zxjpk.setClassess(translateClassess(classess));
		zxjpk.setStep(translateStep(stepName));
		zxjpk.setProjectName(projectName);
		zxjpk.setIsPass(0);
		//这个地方不要忘了-算总分
		zxjpk.setTotalScore(this.getTotalScore(zxjpk));
		return zxjpk;
	}


	@Override
	public Double getTotalScore(Zxjpk zxjpk) {

		double result = 0.0;
		int award = zxjpk.getAward();
		int classess = zxjpk.getClassess();
		int step = zxjpk.getStep();
		if(award == 1 || award == 2){
			if(classess == 1){
				result = 200;
			} else if(classess == 2 ) {
				result = 100;
			} else if(classess == 3 ) {
				result = 10;
			}
		}else if(award == 3){
			if(classess == 1){
				result = 150;
			} else if(classess == 2 ) {
				result = 75;
			} else if(classess == 3 ) {
				result = 10;
			}
		}

		BigDecimal value1 = new BigDecimal( result );
		BigDecimal value2 = new BigDecimal( 2);
		double result2 = value1.divide( value2, 2, BigDecimal.ROUND_HALF_UP ).doubleValue();
		if(step == 2){
			result = result - result2;
		} else if(step ==1){
			result = result2;
		} else if(step == 3){

		} else {
			result = 0;
		}

		return result;

	}



	private int translateClassess(String Classess){
		if(Classess == null || Classess.equals("")){
			return 0;
		} else if(Classess.equals("国家级")){
			return 1;
		} else if(Classess.equals("省部级")){
			return 2;
		} else if(Classess.equals("校级")){
			return 3;
		} else {
			return 0;
		}
	}
	private int translateAward(String award){
		if(award == null || award.equals("")){
			return 0;
		} else if(award.equals("精品在线开放课程")){
			return 1;
		} else if(award.equals("课程思政示范课")){
			return 2;
		} else if(award.equals("创新创业教育课程")){
			return 3;
		} else {
			return 0;
		}
	}

	private Integer translateStep(String stepName) {
		if(stepName == null || stepName.equals("")){
			return 0;
		} else if(stepName.equals("立项")){
			return 1;
		} else if(stepName.equals("结题")){
			return 2;
		} else {
			return 0;
		}
	}

	@Override
	public IPage<ZxjpkMemberPage> getZxjpkMemberPageListByYear(String date, Integer pageNo, Integer pageSize) {
		Page<ZxjpkMemberPage> page = new Page<>(pageNo,pageSize);
		return page.setRecords(this.baseMapper.selectZxjpkMemberPageListByYear(date));
	}

	@Override
	public List<ZxjpkMemberPage> getZxjpkMemberPageListByYear(String date) {
		return this.baseMapper.selectZxjpkMemberPageListByYear(date);
	}


	@Override
	protected List<TeachingResultContent> createTeachingResultContent(Zxjpk jxcg)  {

		List<TeachingResultContent> result = new ArrayList<>();

		// 类型
		TeachingResultContent classTeachingResultContent = new TeachingResultContent();
		classTeachingResultContent.setType(1);
		classTeachingResultContent.setLevel(jxcg.getClassess());

		try {
			classTeachingResultContent.setValue(dictTranslater.getDictValue(ZxjpkMemberPage.class, "classess", String.valueOf(jxcg.getClassess())));
			result.add(classTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		TeachingResultContent stepTeachingResultContent = new TeachingResultContent();
		stepTeachingResultContent.setType(1);
		stepTeachingResultContent.setLevel(jxcg.getStep());

		try {
			stepTeachingResultContent.setValue(dictTranslater.getDictValue(ZxjpkMemberPage.class, "step", String.valueOf(jxcg.getStep())));
			result.add(stepTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		return result;
	}
}
